package dfsketches.gr3d;

public class PolarCamera extends Camera {
	private float dist;
	
	private PolarCamera(float width, float height, Matrix3D matrix, float distance) {
		super(width, height);
		dist = distance;
		updateMatrix();
	}
	
	public PolarCamera(float width, float height, float distance) {
		this(width, height, Matrix3D.getUniteMatrix(), distance);
	}
	
	public PolarCamera(PolarCamera c) {
		this(c.w, c.h, new Matrix3D(c.m), c.dist);
		anim = new CameraAnimation(c.anim);
	}
	
	public void increaseDistance(float increment) {
		dist += increment;
	}
	
	public float getDistance() {
		return dist;
	}
	
	public void setDistance(float distance) {
		dist = distance;
		updateMatrix();
	}
	
	protected void updateMatrix() {
		if (cachedAngleZ != angleZ) {
			cachedAngleZ = angleZ;
			cachedRotZ = Matrix3D.getRotationZMatrix(angleZ);
		}
		if (cachedAngleX != angleX) {
			cachedAngleX = angleX;
			cachedRotX = Matrix3D.getRotationXMatrix(angleX);
		}
		if (cachedAngleY != angleY) {
			cachedAngleY = angleY;
			cachedRotY = Matrix3D.getRotationYMatrix(angleY);
		}
		Matrix3D rot = Matrix3D.getUniteMatrix();
		rot.multiplyRight(cachedRotZ);
		rot.multiplyRight(cachedRotY);
		rot.multiplyRight(cachedRotX);
		
		directZ.setCoordinates(0, 0, 1);
		directZ.apply(Matrix3D.getRotationXMatrix(-angleX));	//TODO: cache it too
		directZ.apply(Matrix3D.getRotationYMatrix(-angleY));
		
		directX.setCoordinates(1, 0, 0);
		directX.apply(Matrix3D.getRotationYMatrix(-angleY));
		
		m.makeTranslation(-pos.x, -pos.y, -pos.z);
		m.multiplyRight(rot);
		m.multiplyRight(Matrix3D.getTranslationMatrix(0f, 0f, dist));
	}

}